package com.hhh.petadopt.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hhh.petadopt.pojo.Admin;
import com.hhh.petadopt.service.adminLoginService;

/**
 * 登录页面
 * @author 杜镇声
 *
 */
@Controller
public class adminLoginController {
	
	@Autowired
	private adminLoginService adminloginservice;
	
	@RequestMapping(value="/admin/adminlog.html")
	public String showLogin(Admin admin,Model model,HttpSession session) {
		String password = DigestUtils.md5DigestAsHex(admin.getAdminpassword().getBytes());
		admin.setAdminpassword(password);
		//判断用户名和密码
	Admin ad = adminloginservice.Login(admin);
		//判断用户是否存在
		if(null == ad) {
			//用户不存在
			//存放提示消息，转发到登录页面
			model.addAttribute("message", "用户名或密码错误");
			return "forward:/admin/login.html";
		}else {
			//用户存在,判断是否有权限
			Integer adminCheck = adminloginservice.adminCheck(ad);
			if(!adminCheck.equals(1)) {
				//没有权限
				//存放错误提示消息，转发到登陆页面
				model.addAttribute("message", "该用户没有权限");
				return "forward:/admin/login.html";
			}else {
				//将用户名保存到session域中
				session.setAttribute("hello", ad.getAdminname());
				return "admin/index";
			}
	
		}
		
		
		

		}
		
		
	
	
	
	
	//登录页面
	@RequestMapping(value="/admin/login.html")
	public String loginpage() {
		return "admin/login";
	}
	
	

}
